
package org.codebase.spring.framework.base.cglib;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import org.codebase.spring.framework.base.bean.Demo;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CreateBean {


    public static Object instantiate(Constructor<?> ctor, Object[] args, Class<?> beanClass) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanClass);
        enhancer.setCallback(new MethodInterceptor() {

            @Override
            public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
                return arg3.invokeSuper(arg0, arg2);
            }
        });
        return (ctor == null) ?
                enhancer.create() :
                enhancer.create(ctor.getParameterTypes(), args);
    }

    public static void main(String[] args) {
        Demo demo = (Demo) CreateBean.instantiate(null, null, Demo.class);
        demo.play1();
    }

}
